package com.kate.mall.promotion.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiOperation;
import com.kate.mall.promotion.entity.PromotionActivityGoodsRelationEntity;
import com.kate.mall.promotion.service.PromotionActivityGoodsRelationService;
import com.kate.mall.common.utils.PageUtils;
import com.kate.mall.common.utils.R;
import io.swagger.annotations.Api;


/**
 * 促销活动跟商品sku的关联关系
 *
 * @author kate-chan
 * @email 857782993@qq.com
 * @date 2022-12-26 16:35:22
 */
@Api(tags = "PromotionActivityGoodsRelationController", description = "促销活动跟商品sku的关联关系服务")
@RestController
@RequestMapping("promotion/promotionactivitygoodsrelation")
public class PromotionActivityGoodsRelationController {
    @Autowired
    private PromotionActivityGoodsRelationService promotionActivityGoodsRelationService;

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("promotion:promotionactivitygoodsrelation:list")
    @ApiOperation("获取所有促销活动跟商品sku的关联关系信息")
    public R list(@RequestParam(required = false) Map<String, Object> params){
        PageUtils page = promotionActivityGoodsRelationService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 根据goodsId返回活动id
     */
    @GetMapping("/goodsId")
    //@RequiresPermissions("promotion:promotionactivitygoodsrelation:list")
    @ApiOperation("获取所有促销活动跟商品sku的关联关系信息")
    public R list(@RequestParam("goods_id") Integer goodsId){
        Optional<Integer> optionalActivityId = promotionActivityGoodsRelationService.findActivityIdByGoodsId(goodsId);
        return R.ok().put("activityId", optionalActivityId.orElse(-1));
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("promotion:promotionactivitygoodsrelation:info")
    @ApiOperation("获取指定id的促销活动跟商品sku的关联关系详情")
    public R info(@PathVariable("id") Long id){
		PromotionActivityGoodsRelationEntity promotionActivityGoodsRelation = promotionActivityGoodsRelationService.getById(id);

        return R.ok().put("promotionActivityGoodsRelation", promotionActivityGoodsRelation);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("添加促销活动跟商品sku的关联关系信息")
    //@RequiresPermissions("promotion:promotionactivitygoodsrelation:save")
    public R save(@RequestBody PromotionActivityGoodsRelationEntity promotionActivityGoodsRelation){
		promotionActivityGoodsRelationService.save(promotionActivityGoodsRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperation("更新指定id促销活动跟商品sku的关联关系信息")
    //@RequiresPermissions("promotion:promotionactivitygoodsrelation:update")
    public R update(@RequestBody PromotionActivityGoodsRelationEntity promotionActivityGoodsRelation){
		promotionActivityGoodsRelationService.updateById(promotionActivityGoodsRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除指定id促销活动跟商品sku的关联关系信息")
    //@RequiresPermissions("promotion:promotionactivitygoodsrelation:delete")
    public R delete(@RequestBody Long[] ids){
		promotionActivityGoodsRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
